#include "zobjects_largenum.h"

ZlangInvokeFunction ZlangInvokeFunction_largeint_Set_int;
int ZlangInvokeFunction_largeint_Set_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largeint	*largeint_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	int32_t					n ;
	
	CallRuntimeFunction_int_GetIntValue( rt , in1 , & n );
	mpz_set_si( largeint_direct_prop->value , (signed long)n );
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_largeint_Set_string;
int ZlangInvokeFunction_largeint_Set_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largeint	*largeint_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	char					*str = NULL ;
	int32_t					str_len ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in1 , & str , & str_len );
	mpz_set_str( largeint_direct_prop->value , str , 10 );
	
	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_largeint;
void *ZlangCreateDirectProperty_largeint( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largeint	*largeint_prop = NULL ;
	
	largeint_prop = (struct ZlangDirectProperty_largeint *)malloc( sizeof(struct ZlangDirectProperty_largeint) ) ;
	if( largeint_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( largeint_prop , 0x00 , sizeof(struct ZlangDirectProperty_largeint) );
	
	mpz_init( largeint_prop->value );
	
	return largeint_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_largeint;
void ZlangDestroyDirectProperty_largeint( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largeint	*largeint_direct_prop = GetObjectDirectProperty(obj) ;
	
	if( largeint_direct_prop )
	{
		mpz_clear( largeint_direct_prop->value );
		
		free( largeint_direct_prop );
	}
	
	return;
}

ZlangToStringFunction ZlangToString_largeint;
int ZlangToString_largeint( struct ZlangRuntime *rt , struct ZlangObject *obj , struct ZlangObject **tostr_obj )
{
	struct ZlangDirectProperty_largeint	*largeint_direct_prop = GetObjectDirectProperty(obj) ;
	char					*to_str = NULL ;
	int32_t					to_str_len ;
	
	to_str = mpz_get_str( NULL , 10 , largeint_direct_prop->value ) ;
	to_str_len = (int32_t)strlen( to_str ) ;
	CallRuntimeFunction_string_SetStringValue( rt , (*tostr_obj) , to_str , to_str_len );
	free( to_str );
	
	return 0;
}

ZlangFromDataPtrFunction ZlangFromDataPtr_largeint;
int ZlangFromDataPtr_largeint( struct ZlangRuntime *rt , struct ZlangObject *obj , void *value_ptr , int32_t value_len )
{
	struct ZlangDirectProperty_largeint	*obj_direct_prop = GetObjectDirectProperty(obj) ;
	
	mpz_set( obj_direct_prop->value , *(mpz_t *)value_ptr );
	
	return 0;
}

ZlangGetDataPtrFunction ZlangGetDataPtr_largeint;
int ZlangGetDataPtr_largeint( struct ZlangRuntime *rt , struct ZlangObject *obj , void **value_ptr , int32_t *value_len )
{
	struct ZlangDirectProperty_largeint	*obj_direct_prop = GetObjectDirectProperty(obj) ;
	
	if( value_ptr )
		(*value_ptr) = & (obj_direct_prop->value) ;
	if( value_len )
		(*value_len) = sizeof(obj_direct_prop->value) ;
	return 0;
}

ZlangOperatorFunction ZlangOperator_largeint_PLUS_largeint;
int ZlangOperator_largeint_PLUS_largeint( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largeint	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largeint	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largeint	*out = NULL ;
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpz_add( out->value , in1->value , in2->value );
	
	return 0;
}

ZlangOperatorFunction ZlangOperator_largeint_MINUS_largeint;
int ZlangOperator_largeint_MINUS_largeint( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largeint	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largeint	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largeint	*out = NULL ;
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpz_sub( out->value , in1->value , in2->value );
	
	return 0;
}

ZlangOperatorFunction ZlangOperator_largeint_MUL_largeint;
int ZlangOperator_largeint_MUL_largeint( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largeint	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largeint	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largeint	*out = NULL ;
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpz_mul( out->value , in1->value , in2->value );
	
	return 0;
}

ZlangOperatorFunction ZlangOperator_largeint_DIV_largeint;
int ZlangOperator_largeint_DIV_largeint( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largeint	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largeint	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largeint	*out = NULL ;
	
	if( mpz_get_ui(in2->value) == 0 )
	{
		TEST_RUNTIME_DEBUG_THEN_PRINT( rt , "*** FATAL : division by zero" )
		return ThrowFatalException( rt , EXCEPTION_CODE_GENERAL , EXCEPTION_MESSAGE_DIVISION_BY_ZERO );
	}
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpz_cdiv_q( out->value , in1->value , in2->value );
	
	return 0;
}

ZlangOperatorFunction ZlangOperator_largeint_MOD_largeint;
int ZlangOperator_largeint_MOD_largeint( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largeint	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largeint	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largeint	*out = NULL ;
	
	if( mpz_get_ui(in2->value) == 0 )
	{
		TEST_RUNTIME_DEBUG_THEN_PRINT( rt , "*** FATAL : division by zero" )
		return ThrowFatalException( rt , EXCEPTION_CODE_GENERAL , EXCEPTION_MESSAGE_DIVISION_BY_ZERO );
	}
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpz_cdiv_r( out->value , in1->value , in2->value );
	
	return 0;
}

static struct ZlangDirectFunctions direct_funcs_largeint =
	{
		ZLANG_OBJECT_largeint , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_largeint , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_largeint , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		ZlangToString_largeint , /* ZlangToStringFunction *to_string_func */
		ZlangFromDataPtr_largeint , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		ZlangGetDataPtr_largeint , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		ZlangOperator_largeint_PLUS_largeint , /* ZlangOperatorFunction *oper_PLUS_func */
		ZlangOperator_largeint_MINUS_largeint , /* ZlangOperatorFunction *oper_MINUS_func */
		ZlangOperator_largeint_MUL_largeint , /* ZlangOperatorFunction *oper_MUL_func */
		ZlangOperator_largeint_DIV_largeint , /* ZlangOperatorFunction *oper_DIV_func */
		ZlangOperator_largeint_MOD_largeint , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		NULL , /* ZlangSummarizeDirectPropertySizeFunction */
	} ;

ZlangImportObjectFunction ZlangImportObject_largeint;
struct ZlangObject *ZlangImportObject_largeint( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_largeint , & direct_funcs_largeint , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_largeint_Set_int , ZLANG_OBJECT_void , "Set" , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_largeint_Set_string , ZLANG_OBJECT_void , "Set" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj;
}

